#include "thread.h"

Thread::Thread()
{
    m_base=event_base_new();
    if(!m_base)
    {

    }
   int fd[2];
   if(pipe(fd)==-1)
   {
       qDebug()<<" pipe error\n";
   }
   piperead=fd[0];
   pipewrite=fd[1];

   event_set(&m_pipeevent,piperead,EV_READ|EV_PERSIST,readpipefd,this);
   event_base_set(m_base,&m_pipeevent);
   event_add(&m_pipeevent,0);
}

void Thread::start()
{
    pthread_create(&m_pthreadid,NULL,worker,this);
}

event_base *Thread::getbase()
{

    return m_base;
}

void* Thread::worker(void *arg)
{
    Thread*p=(Thread*)arg;
    p->run();


}

void Thread::readpipefd(evutil_socket_t fd, short s, void *arg)
{


}

void Thread::run()
{  /* while(1)
    {
        qDebug()<<"pthreadid:"<<m_pthreadid<<"\n";
        sleep(2);
    }*/

    event_base_dispatch(m_base);
    event_base_free(m_base);

}
